package com.cdbkj.cloud.pos;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.XXX.socialsecuritycard.impl.ISocialSecurityInterface;
import com.androidyang.library.common.utils.GsonUtils;
import com.androidyang.library.common.utils.ToastUtils;
import com.androidyang.library.http.okhttp.bean.HttpResult;
import com.androidyang.library.http.okhttp.listener.HttpClientHandler;
import com.cdbkj.cloud.pos.bean.Filed;
import com.cdbkj.cloud.pos.bean.PrintModel;
import com.cdbkj.cloud.pos.bean.RegInfoForm;
import com.cdbkj.cloud.pos.fragment.ConfirmRegFragment;
import com.cdbkj.cloud.pos.fragment.DoctorFragment;
import com.cdbkj.cloud.pos.fragment.IdentificationFragment;
import com.cdbkj.cloud.pos.fragment.MedicalCardFragment;
import com.cdbkj.cloud.pos.fragment.PayWayFragment;
import com.cdbkj.cloud.pos.fragment.RegDateFragment;
import com.cdbkj.cloud.pos.fragment.RegTypeFragment;
import com.cdbkj.cloud.pos.fragment.WarnFragment;
import com.cdbkj.cloud.pos.fragment.WorkTimeFragment;
import com.cdbkj.cloud.pos.http.HttpRequest;
import com.cdbkj.cloud.pos.utils.DateUtils;
import com.cdbkj.cloud.pos.utils.HardwareUtil;
import com.cdbkj.cloud.pos.utils.Tools;
import com.dawn.cloud.pos.drive.sdk.BmpSdk;
import com.dawn.cloud.pos.drive.sdk.PrintSdk;
import com.cdbkj.cloud.pos.bean.JsonResult;
import com.cdbkj.cloud.pos.fragment.BankCardFragment;
import com.cdbkj.cloud.pos.fragment.DepartmentFragment;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class RegActivity extends AppCompatActivity implements
        RegDateFragment.OnRegDateFragmentInteractionListener,
        RegTypeFragment.OnRegTypeFragmentInteractionListener,
        DepartmentFragment.OnDeptFragmentInteractionListener,
        DoctorFragment.OnDoctorFragmentInteractionListener,
        IdentificationFragment.OnIdentificationFragmentInteractionListener,
        ConfirmRegFragment.OnConfirmRegFragmentInteractionListener,
        PayWayFragment.OnPayWayFragmentInteractionListener,
        BankCardFragment.OnBankCardFragmentInteractionListener,
        MedicalCardFragment.OnMedicalCardFragmentInteractionListener,
        View.OnClickListener, HttpClientHandler {

    private FragmentManager fragmentManager = getSupportFragmentManager();
    private RegActivity activity = this;

    private Timer timer = new Timer();
    private String userId = Tools.getAndroidOsSystemProperties();//产品sn作为序列号
    private String httpType;

    private String orderNumber;

    private TextView textView_regDate;
    private TextView textView_regType;
    private TextView textView_dept;
    private TextView textView_workTime;
    private TextView textView_doctor;
    private TextView textView_confirmReg;
    private TextView textView_payWay;
    private ImageView homeImage;
    private int num = 0;
    private TextView retentionTime_tv;

    private RegInfoForm regInfoForm;
    private Runnable handelBizRunnable = null;//业务处理进程
    private Runnable retentionRunnable = null;//页面滞留时间进程
    //private int retentionTime = Filed.retentionTime;//页面滞留时间
    private int readCardTime = Filed.readCardTime;//读卡间隔时间

    //配对蓝牙名称
    private String bluetoothName;
    //配对款台号
    private String counterNo;

    private BmpSdk bmpSdk;

    private BankCardFragment bankCardFragment;

    private String hisTime;

    private String pwd = "";

    private String regTypeId;

    private int queryWxOrderPayStatusCount = 0;

    private AlertDialog alertDialog1 = null;
    //是否支付中
    private boolean isPaying = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);

        // 获取意图对象
        Intent intent = getIntent();
        //获取传递的值
        bluetoothName = intent.getStringExtra("bluetoothName");
        counterNo = intent.getStringExtra("counterNo");
        hisTime = intent.getStringExtra("hisTime");

        regInfoForm = new RegInfoForm();
        regInfoForm.setUserId(userId);

        TextView textBtn = findViewById(R.id.returnBtn);
        textBtn.setOnClickListener(this);
        homeImage = findViewById(R.id.home_btn);
        homeImage.setOnClickListener(this);

        textView_regDate = findViewById(R.id.select_date_tab);
        textView_regType = findViewById(R.id.select_type_tab);
        textView_dept = findViewById(R.id.select_dept_tab);
        //      textView_workTime = findViewById(R.id.select_work_time_tab);
        textView_doctor = findViewById(R.id.select_doctor_tab);
        textView_confirmReg = findViewById(R.id.confirm_reg_tab);
        textView_payWay = findViewById(R.id.select_pay_way);

        retentionTime_tv = findViewById(R.id.retentionTime_tv);
        retentionTime_tv.setText((Filed.retentionTime / 1000) + " 秒");

        this.intoFirstFragment();
    }

    /**
     * 挂号类别
     */
    public void intoFirstFragment() {
        textView_regType.setActivated(true);
        getSupportFragmentManager().beginTransaction().replace(R.id.contentPanel, RegTypeFragment.newInstance(userId)).addToBackStack(null).commit();
        this.handelRetention(this, "");
    }

    /**
     * 选择日期
     */
    @Override
    public void onRegTypeFragmentInteraction(String regTypeId, String regType) {
        regInfoForm.setRegTypeId(regTypeId);
        regInfoForm.setRegType(regType);
        if (regTypeId.equals("急诊号")) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            this.onRegDateFragmentInteraction(userId, format.format(new Date()), regTypeId);
            return;
        }
        textView_regDate.setActivated(true);
        getSupportFragmentManager().beginTransaction().replace(R.id.contentPanel, RegDateFragment.newInstance(Filed.userId, regTypeId)).addToBackStack(null).commit();
        this.handelRetention(this, "");
    }

    //返回选择挂号类别界面
    @Override
    public void onRegReturnRegDate() {
        getSupportFragmentManager().beginTransaction().replace(R.id.contentPanel, RegTypeFragment.newInstance(userId)).addToBackStack(null).commit();
        this.handelRetention(this, "");
    }

    /**
     * 选择科室
     */
    @Override
    public void onRegDateFragmentInteraction(String userId, String regDate, String regTypeId) {
        regInfoForm.setRegDate(regDate);
        textView_dept.setActivated(true);
        getSupportFragmentManager().beginTransaction().replace(R.id.contentPanel, DepartmentFragment.newInstance(Filed.userId, regDate, regTypeId)).addToBackStack(null).commit();
        this.handelRetention(this, "");
    }


    @Override
    public void onRegTypeJumpPreFragmentInteraction() {
        textView_regType.setActivated(false);
        fragmentManager.popBackStack();
        this.intoFirstFragment();
    }


    /**
     * 号源列表
     */
    @Override
    public void onDeptFragmentInteraction(String departmentID, String departmentName) {
        regInfoForm.setDepartmentId(departmentID);
        regInfoForm.setDepartmentName(departmentName);
     /*   if(regInfoForm.getRegTypeId().equals("急诊号")){
         //   this.onDoctorFragmentInteraction();
            return;
        }*/

        textView_doctor.setActivated(true);
        getSupportFragmentManager().beginTransaction().replace(R.id.contentPanel, DoctorFragment.newInstance(userId, regInfoForm.getRegDate(), regInfoForm.getRegTypeId(), departmentID, departmentName)).addToBackStack(null).commit();
        this.handelRetention(this, "");
    }

    @Override
    public void onDeptFragmentJumpPreInteraction() {
        textView_dept.setActivated(false);
        fragmentManager.popBackStack();
        this.onRegTypeFragmentInteraction(regInfoForm.getRegTypeId(), regInfoForm.getRegType());
    }


    /**
     * 进入第6个界面 身份识别
     */
    @Override
    public void onDoctorFragmentInteraction(String doctorId, String doctorName, String totalCost) {
        regInfoForm.setDoctorId(doctorId);
        regInfoForm.setDoctorName(doctorName);
        regInfoForm.setTotalCost(totalCost);
        getSupportFragmentManager().beginTransaction().replace(R.id.contentPanel, IdentificationFragment.newInstance()).addToBackStack(null).commit();
        final RegActivity regActivity = this;
        this.getPatientId(regActivity);
        this.handelRetention(this, "未获取到用户信息！");
    }

    //扫码
    public void getPatientId(final RegActivity regActivity) {
        timer = new Timer();
        final TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                EditText editText = findViewById(R.id.getPatientId);
                num++;
                System.out.println("我是扫描线程，我没有停止。。。");
                System.out.println("我是扫描线程，我没有停止。。。");
                System.out.println("我是扫描线程，我没有停止。。。");
                if (num == 35) {
                    num = 0;
                    timer.cancel();
                    cancel();
                    getSupportFragmentManager().beginTransaction().replace(R.id.contentPanel, DoctorFragment.newInstance(userId, regInfoForm.getRegDate(), regInfoForm.getRegTypeId(), regInfoForm.getDepartmentId(), regInfoForm.getDepartmentName())).addToBackStack(null).commit();
                }
                try {
                    if (!editText.getText().toString().equals("")) {
                        String patientId = editText.getText().toString();
                        String reg = "[^(a-zA-Z0-9\\\\u4e00-\\\\u9fa5)]";
                        patientId = patientId.replaceAll(reg, "");
                        patientId = patientId.replaceAll("\\(", "");
                        regInfoForm.setPatientId(editText.getText().toString());
                        regInfoForm.setCardTypeId("1");
                        //读取卡号成功后，读取档案信息
                        httpType = "patientInfo";
                        textView_doctor.removeCallbacks(handelBizRunnable);
                        HttpRequest.patientInfo("patientInfo", regActivity, patientId, userId);
                        System.out.println(editText.getText());
                        timer.cancel();
                        cancel();
                    }
                } catch (Exception e) {
                    timer.cancel();
                    cancel();
                }
            }
        };
        timer.schedule(timerTask, 500, 1000);
    }

    /**
     * 返回科室页面
     */
    @Override
    public void onDoctorFragmentJumpPreInteraction() {
        textView_doctor.setActivated(false);
        fragmentManager.popBackStack();
        fragmentManager.popBackStack();
        getSupportFragmentManager().beginTransaction().replace(R.id.contentPanel, DepartmentFragment.newInstance(Filed.userId, regInfoForm.getRegDate(), regInfoForm.getRegTypeId())).addToBackStack(null).commit();
    }

    /**
     *  确认挂号
     */
    @Override
    public void onIdentificationFragmentInteraction() {
        textView_confirmReg.setActivated(true);
        getSupportFragmentManager().beginTransaction().replace(R.id.contentPanel, ConfirmRegFragment.newInstance(regInfoForm)).addToBackStack(null).commit();

        this.handelRetention(this, "");
    }

    /**
     *  选择支付方式
     */
    @Override
    public void onConfirmRegFragmentInteraction(String type, String registerId) {
        if ("confirm".equals(type)) {
            regInfoForm.setRegisterId(registerId);
            textView_payWay = findViewById(R.id.select_pay_way);
            textView_payWay.setActivated(true);
            getSupportFragmentManager().beginTransaction().replace(R.id.contentPanel, PayWayFragment.newInstance(null, null)).addToBackStack(null).commit();
        } else if ("cancel".equals(type)) {
           Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }

        this.handelRetention(this, "");
    }

    /**
     * 进入第9个界面 支付
     */
    @Override
    public void onPayWayFragmentInteraction(String type) {
        isPaying = true;
        if ("bank_card".equals(type)) {
            //进入第6_1个界面 银行卡支付
            bmpSdk = new BmpSdk();
            bankCardFragment = BankCardFragment.newInstance(userId, regInfoForm.getTotalCost(), bluetoothName, counterNo);
            bankCardFragment.setBmpSdk(bmpSdk);
            getSupportFragmentManager().beginTransaction().replace(R.id.contentPanel, bankCardFragment).addToBackStack(null).commit();
        } else if ("medical_card".equals(type)) {
            isPaying = false;
            //进入第6_2个界面 医保卡支付
            getSupportFragmentManager().beginTransaction().replace(R.id.contentPanel, MedicalCardFragment.newInstance(null, null)).addToBackStack(null).commit();
            //处理医保卡交易及调用读卡器驱动
           /* this.onMedicalCardFragmentInteraction(pwd);*/
            //  Tools.showDialog("挂号，不支持医支付",this);
        } else if ("scan_qrcode".equals(type)) {
            httpType = "addRegOrder";
            HttpRequest.addRegOrder("addRegOrder", this, regInfoForm);
        }

        this.handelRetention(this, "");
    }

    /**
     * 挂号
     */
    @Override
    public void onBankCardFragmentInteraction(String posTransNo, String openBank, String openAccount) {
        httpType = "modifyReg";
        HttpRequest.modifyReg("modifyReg", this, regInfoForm.getRegisterId(), regInfoForm.getPatientId(), "银行卡支付", "4", posTransNo, openBank, openAccount);
    }

    @Override
    public void returnSelectPayWay() {
        this.onConfirmRegFragmentInteraction("confirm", regInfoForm.getRegisterId());
    }

    @Override
    public void onBankCardRescissionInteraction(String status) {
        httpType = "modifyRegRescissionStatus";
        HttpRequest.modifyRegRescissionStatus("modifyRegRescissionStatus", this, status, regInfoForm.getRegisterId());
    }

    /**
     * 处理医保卡交易及调用读卡器驱动
     */
    @Override
    public void onMedicalCardFragmentInteraction(String pwd, FragmentActivity activity) {

        //读卡获取信息
        //  String result = readMedicalCard(pwd);
        //1、调用医保卡支付处理方法
        Map resultArgs = HardwareUtil.handelMedicalInsuranceCard(regInfoForm.getTotalCost());
        String siOutput = resultArgs.get("siOutput").toString();
        String siInputInfo = resultArgs.get("siInputInfo").toString();
        String siPatientInfo = resultArgs.get("siPatientInfo").toString();
        //2、处理挂号支付信息
        if ((Boolean) resultArgs.get("result")) {
            httpType = "modifyReg";
            HttpRequest.modifyRegWithYb("modifyRegWithYb", this, regInfoForm.getRegisterId(), regInfoForm.getPatientId(), siOutput, siInputInfo, siPatientInfo);
        }
    }

    @Override
    public void onClick(View view) {
        if (retentionRunnable != null) {
            homeImage.removeCallbacks(retentionRunnable);
        }
        if (handelBizRunnable != null) {
            textView_doctor.removeCallbacks(handelBizRunnable);
        }
        if (bmpSdk != null) {
            bmpSdk.shutdownClient();
            //解除广播
            unregisterReceiver(bmpSdk);
            bmpSdk = null;
        }
        switch (view.getId()) {
            case R.id.returnBtn:
                if (isPaying) {
                    return;
                }
                int backStackEntryCount = fragmentManager.getBackStackEntryCount();
                if(regInfoForm.getRegTypeId().equals("急诊号")){
                    if (backStackEntryCount == 7) {
                        textView_payWay.setActivated(false);
                        textView_confirmReg.setActivated(false);
                        fragmentManager.popBackStack();
                        fragmentManager.popBackStack();
                    }
                    if (backStackEntryCount == 6) {
                        textView_payWay.setActivated(false);
                        textView_confirmReg.setActivated(false);
                        fragmentManager.popBackStack();
                    }
                    if (backStackEntryCount == 4) {
                     //   textView_doctor.setActivated(false);
                    }

                    if (backStackEntryCount == 3) {
                        textView_doctor.setActivated(false);
                    }
                    if (backStackEntryCount == 2) {
                        textView_dept.setActivated(false);
                    }
                    if (backStackEntryCount == 4) {

                        timer.cancel();
                    }
                    if (backStackEntryCount > 1) {
                        fragmentManager.popBackStack();
                        this.handelRetention(this, "");
                    } else {
                        finish();
                    }
                    break;
                }

                if (backStackEntryCount == 7) {
                    textView_payWay.setActivated(false);
                    textView_confirmReg.setActivated(false);
                    fragmentManager.popBackStack();
                    fragmentManager.popBackStack();
                }
                if (backStackEntryCount == 6) {
                    textView_confirmReg.setActivated(false);
                    fragmentManager.popBackStack();
                }
                if (backStackEntryCount == 4) {
                    textView_doctor.setActivated(false);
                }
              /*  if (backStackEntryCount == 4) {
                    textView_workTime.setActivated(false);
                }*/
                if (backStackEntryCount == 3) {
                    textView_dept.setActivated(false);
                }
                if (backStackEntryCount == 2) {
                    textView_regDate.setActivated(false);
                }
                if (backStackEntryCount == 5) {
                    timer.cancel();
                }
                if (backStackEntryCount > 1) {
                    fragmentManager.popBackStack();
                    this.handelRetention(this, "");
                } else {
                    finish();
                }
                break;
            case R.id.home_btn:
                timer.cancel();
                if (isPaying) {
                    return;
                }

                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                break;
        }
    }

    /**
     * 处理页面滞留
     */
    public void handelRetention(final RegActivity regActivity, final String msg) {
        if (retentionRunnable != null) {
            homeImage.removeCallbacks(retentionRunnable);
        }
        Tools.retentionTime = Filed.retentionTime;//重置 页面滞留时间
        retentionRunnable = new Runnable() {
            @Override
            public void run() {
                if (Tools.retentionTime <= 0) {
                    isPaying = false;
                    if (!"".equals(msg)) {
                        Tools.showDialog(msg, regActivity);
                    }
                    homeImage.removeCallbacks(handelBizRunnable);
                    if (handelBizRunnable != null) {
                        textView_doctor.removeCallbacks(handelBizRunnable);
                    }
                    ImageView home_btn = findViewById(R.id.home_btn);
                    home_btn.performClick();
                } else {
                    Tools.retentionTime -= 1000;
                    retentionTime_tv.setText(Tools.retentionTime / 1000 + " 秒");
                    //每1秒执行一次
                    homeImage.postDelayed(this, 1000);
                }
            }
        };
        homeImage.postDelayed(retentionRunnable, 0);
    }

    @Override
    public void httpClientSuccess(Object o, HttpResult httpResult) {
        JsonResult json = (JsonResult) httpResult.getResult();
        if (json == null) {
            //ToastUtils.showShort(this,"服务器处理异常");
            Tools.showDialogWithReturnMainActivity("服务器处理异常", this);
        }

        if ("patientInfo".equals(httpType)) {
            if (!json.isSuccess()) {
                //ToastUtils.showShort(this,json.getMsg());
                View.OnClickListener clickListener = new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Tools.dismissDialog();
                        fragmentManager.popBackStack();
                        getSupportFragmentManager().beginTransaction().replace(R.id.contentPanel, DoctorFragment.newInstance(userId, regInfoForm.getRegDate(), regInfoForm.getRegTypeId(), regInfoForm.getDepartmentId(), regInfoForm.getDepartmentName())).addToBackStack(null).commit();
                    }
                };
                Tools.showDialog(json.getMsg() + "请重新扫描", this, clickListener, clickListener);
                return;
            }

            Map resultMap = (Map) json.getData();
            if (resultMap.get("patientId") == null) {
                //ToastUtils.showShort(this,"HIS数据错误，无病人ID");
                Tools.showDialogWithReturnMainActivity("HIS数据错误，无病人ID", this);
                return;
            }
            if (resultMap.get("patientName") == null) {
                //ToastUtils.showShort(this,"HIS数据错误，无病人ID");
                Tools.showDialogWithReturnMainActivity("HIS数据错误，无病人名称", this);
                return;
            }
            if (resultMap.get("idCardNo") != null) {
                regInfoForm.setIdCardNo(resultMap.get("idCardNo").toString());
            }
            regInfoForm.setPatientId(resultMap.get("patientId").toString());
            regInfoForm.setPatientName(resultMap.get("patientName").toString());
            //跳转确认挂号
            this.onIdentificationFragmentInteraction();
        } else if ("modifyReg".equals(httpType)) {
            isPaying = false;
            if (!json.isSuccess()) {
                //ToastUtils.showShort(this,json.getMsg());
                //Tools.showDialog(json.getMsg(),this);
                if ("modifyReg".equals(httpType)) {
                    //  bankCardFragment.rescission();
                }
                return;
            }
            Map resultMap = (Map) json.getData();
            String tranFlow = "";
            String registerNo = "";
            String regTime = "";
            if (resultMap.get("tranFlow") != null) {
                tranFlow = resultMap.get("tranFlow").toString();
            }
            if (resultMap.get("registerNo") != null) {
                registerNo = resultMap.get("registerNo").toString();
            }
            if (resultMap.get("regTime") != null) {
                regTime = resultMap.get("regTime").toString();
            }

            //跳转挂号成功
            getSupportFragmentManager().beginTransaction().replace(R.id.contentPanel, WarnFragment.newInstance("挂号成功，谢谢您的使用！")).addToBackStack(null).commit();

            print(registerNo, regTime);
        } else if ("addRegOrder".equals(httpType)) {
            if (!json.isSuccess()) {
                //ToastUtils.showShort(this,json.getMsg());
                Tools.showDialog(json.getMsg(), this);
                return;
            }
            orderNumber = (String) json.getData();
            //String imgUrl = HttpUrls.QRCODE_PATH+"/"+Filed.wx_app_id+"/"+orderNumber+"/"+new Double(Double.parseDouble(regInfoForm.getTotalCost())*100).longValue()+"/挂号支付";
            Tools.showImgDialog("请用微信扫-扫", HttpRequest.getQRCodeUrl(orderNumber, Double.parseDouble(regInfoForm.getTotalCost()), "挂号支付"), this);
            this.handelRetention(this, "");

            //查询订单支付状态
            handelBizRunnable = new Runnable() {
                @Override
                public void run() {
                    if (queryWxOrderPayStatusCount >= 60) {
                        queryWxOrderPayStatusCount = 0;
                        textView_doctor.removeCallbacks(handelBizRunnable);
                        return;
                    }
                    queryWxOrderPayStatusCount++;
                    httpType = "wxOrderPayStatus";
                    HttpRequest.getWxOrderPayStatus("wxOrderPayStatus", activity, orderNumber);
                    textView_doctor.postDelayed(handelBizRunnable, 5000);
                }
            };
            textView_doctor.postDelayed(handelBizRunnable, 5000);
        } else if ("wxOrderPayStatus".equals(httpType)) {
            Map resultMap = (Map) json.getData();
            if (resultMap.get("trade_state") != null && "SUCCESS".equals(resultMap.get("trade_state").toString())) {
                textView_doctor.removeCallbacks(handelBizRunnable);
                Tools.dismissImgDialog();
                //更新订单支付状态
                httpType = "modifyOrder";
                HttpRequest.modifyRegOrderPayStatus("modifyRegOrderPayStatus", activity, orderNumber);
            }
        } else if ("modifyOrder".equals(httpType)) {
            isPaying = false;
            if (!json.isSuccess()) {
                //ToastUtils.showShort(this,json.getMsg());
                httpType = "refundWxOrder";
                HttpRequest.refundWxOrder("refundWxOrder", activity, orderNumber, Double.parseDouble(regInfoForm.getTotalCost()));
                Tools.showDialog("费用已经退回到您的微信！（挂号失败，原因：" + json.getMsg() + "）", this);
                return;
            }
            //跳转挂号成功
            getSupportFragmentManager().beginTransaction().replace(R.id.contentPanel, WarnFragment.newInstance("挂号成功，谢谢您的使用！")).addToBackStack(null).commit();
            //打印挂号单据
            Map resultMap = (Map) json.getData();
            String tranFlow = "";
            String registerNo = "";
            String regTime = "";
            if (resultMap.get("tranFlow") != null) {
                tranFlow = resultMap.get("tranFlow").toString();
            }
            if (resultMap.get("registerNo") != null) {
                registerNo = resultMap.get("registerNo").toString();
            }
            if (resultMap.get("regTime") != null) {
                regTime = resultMap.get("regTime").toString();
            }
            print(registerNo, regTime);
        }
    }

    @Override
    public void httpClientError(Object o, HttpResult httpResult) {
        //ToastUtils.showShort(this,"网络异常");
        Tools.showDialogWithReturnMainActivity("网络连接异常，请联系管理员", this);
    }

    private void print(String registerNo, String regTime) {
        //打印挂号单据
        List<PrintModel> list = new ArrayList<>();
        //list.add(new PrintModel("流水号",tranFlow));
        list.add(new PrintModel("就诊号", registerNo));
        list.add(new PrintModel("姓名", regInfoForm.getPatientName()));
        list.add(new PrintModel("挂号科室", regInfoForm.getDepartmentName()));
        list.add(new PrintModel("看诊日期", regInfoForm.getRegDate()));
        list.add(new PrintModel("金额", regInfoForm.getTotalCost() + " 元"));

        String receiptType = Filed.RECEIPT_TYPE_PAY_REGISTER;
        String title = "德宏州人名医院人民医院";
        String regDate = hisTime;
        String patientId = regInfoForm.getPatientId();
        PrintSdk.print(list, receiptType, title, regDate, patientId, "挂号成功，请您准时到相应诊室候诊");
    }

    private String readMedicalCard(String pwd) {
        for (int i = 0; i < 10; i++) {
            String result = "";
            ISocialSecurityInterface iSocialSecurityInterface = new ISocialSecurityInterface();
            JSONObject jsonObject = new JSONObject();
            int t = iSocialSecurityInterface.load(pwd, 100000, jsonObject);
            if (t == 0) {
                result = GsonUtils.toJson(jsonObject);
                return result;
            }
            try {
                long time = 2000;
                Thread.sleep(time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Tools.showDialogWithReturnMainActivity("医保结算超时", activity);
        return "";
    }


}
